#!/usr/bin/perl
use strict;
use warnings;
use JIRA::Client;
use Data::Dumper;
use Config::Properties;

my $REPOS = shift;
my $TXN= shift;
#open my $fh, '<', '../conf/info.props' or die "unable to open"; 
open my $fh, "<$REPOS/conf/info.props" || die "Cannot open config $!";
my $properties= Config::Properties->new();
$properties->load($fh);
my $user= $properties->getProperty("user");
my $pass= $properties->getProperty("pass"); 

my @svnlook= `/usr/bin/svnlook log -t $TXN $REPOS`;
my $svnusername= `/usr/bin/svnlook author -t $TXN $REPOS`; #print STDERR "username is: ",$svnusername;

chomp($svnusername);

sub checkticket
{
 my $ticketnumber= shift;
 my $jira = JIRA::Client->new('http://jira.addteq.com', $user, $pass);
 my $issue = eval { $jira->getIssue($ticketnumber) };
#   exit 1; 
 die "Can't getIssue(): $@" if $@;
return $issue;
}


foreach (@svnlook)
{
  chomp($_);
  my $ticketnumber;  
 
  if(($_ =~ m/EMSPERL-\d+/)|| ($_=~ m/CMUTIL-\d+/))
   {
     ($ticketnumber) = ($_) =~ m/(CMUTIL-\d+|EMSPERL-\d+)/i;
     #  print STDERR $ticketnumber, "\n";
     #exit 1;
     
     my $issue = checkticket($ticketnumber); 
     #print STDERR Dumper($issue);
     #exit 1; 
     #print STDERR "assignee is: ", $issue->{'assignee'};
     #exit 1;

     my $assignee= $issue->{'assignee'};
     
     if ($assignee eq $svnusername)
     {
       # print STDERR "same user";
       # exit 1;       
     }
     else
     {
         print STDERR "You are not assigned this ticket! Cannot Commit.";
         exit 1;
     }
   }
   else
   {    
   print STDERR "Error: No ticket number found, cannot commit";
   exit 1; 
   }
}
exit 0;
